/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.jet.ui;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

import edu.pku.sei.modeler.jet.codegen.GenBundleCode;
import edu.pku.sei.modeler.jet.codegen.GenProjStructure;
import edu.pku.sei.modeler.jet.codegen.GenSampleActions;
import edu.pku.sei.modeler.jet.util.ModelerConstants;
import edu.pku.sei.modeler.jet.util.PathDescriptor;

/**
 * @author zl
 * 
 */
public class ActionGenWizard extends Wizard implements INewWizard {
	private SetProjectNameDialog SetNameD;

	public ActionGenWizard() {
		setNeedsProgressMonitor(true);
	}

	public void addPages() {
		SetNameD = new SetProjectNameDialog("Project name");
		SetNameD.setProjectName(ModelerConstants.projName);
		SetNameD.setNeedModel(false);
		SetNameD.setModel(null);
		addPage(SetNameD);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	@Override
	public boolean performFinish() {
		PathDescriptor p = new PathDescriptor();
		String templatesBase = p.getAbsolutePath();
		
		final GenProjStructure gps = new GenProjStructure(null);
		gps.setProjName(SetNameD.name);
		final GenBundleCode gbc = new GenBundleCode(SetNameD.getBundleInfo());
    	gbc.setTemplateBase(templatesBase);
		final GenSampleActions gsa = new GenSampleActions();
		gsa.setTemplateBase(templatesBase);
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor)
					throws InvocationTargetException {
				try {
					monitor.setTaskName("creating project");
					gps.generateCode(monitor);
					monitor.setTaskName("generating bundle code");
			    	gbc.setProject(gps.getProject());
			    	gbc.generateCode(monitor);
					monitor.setTaskName("generating actions code");
					gsa.setProject(gps.getProject());
					gsa.generateCode(monitor);
					monitor.done();
				} catch (InterruptedException e) {
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}
		};
		try {
			getContainer().run(true, false, op);
		} catch (InterruptedException e) {
			e.printStackTrace();
			return false;
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
	 *      org.eclipse.jface.viewers.IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		// TODO Auto-generated method stub

	}

}
